package com.qf.controller;

import com.qf.model.Student;
import com.qf.service.StudentSerivce;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author GUOFUCHENG
 * @date 2025/8/12
 * @desc
 */
@RestController
public class StudentController {
    @Autowired
    private StudentSerivce studentSerivce;

    //查询一个数据(通过id)
    @GetMapping("/stu/{id}")
    public R findByid(@PathVariable int id) {
        Student student = studentSerivce.findByid(id);
        return R.success(student);
    }

    //查询全部数据
    @GetMapping("/stu/findAll")
    public R findAll() {
        List<Student> students = studentSerivce.findAll();
        return R.success(students);
    }

    //模拟搜索框根据年龄或者姓名查询学生
    @GetMapping("/search")
    public R findByKeyWord(@RequestParam HashMap<Object, Object> map) {
        List<Student> students = studentSerivce.findByKeyWord(map);
        return R.success(students);
    }

    //增加一个学生对象
    @GetMapping("/add")
    public R add(Student student) {
        boolean result = studentSerivce.add(student);
        return R.success(result);
    }


    //修改学生信息(先根据学号判断学生是否存在,存在则修改)
    @GetMapping("/edit/")
    public R edit(Student student) {
        Student stu = studentSerivce.findByid(student.getId());
        System.out.println("student = " + student.getId());
        System.out.println("stu = " + stu);
        if (stu == null) {
            return R.fail();
        }
        stu.setSname(student.getSname());
        stu.setAge(student.getAge());
        stu.setSex(student.getSex());
        stu.setScore(student.getScore());
        stu.setBirthday(student.getBirthday());
        int i = studentSerivce.edit(stu);
        return R.success(i);
    }

    //删除学生信息(通过id进行删除)
    @GetMapping("/delete/{id}")
    public R delById(@PathVariable int id) {
        int i = studentSerivce.delById(id);
        return R.success(i);
    }

    //批量删除 学生信息(通过学生id)
    @PostMapping("/delete/ids")
    public R delByIds(@RequestParam List<Object> ids) {
        System.out.println("ids = " + ids);
        int i = studentSerivce.delByIds(ids);
        return R.success(i);
    }


}
